package com.zlx.xBiaoShop.service;


import com.zlx.xBiaoShop.dao.UserDao;
import com.zlx.xBiaoShop.entity.User;
import com.zlx.xBiaoShop.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description user 服务层
 * @author admin
 * @date 2020-12-05 17:52:12
*/
@Service
public class UserService {

	@Autowired
	private UserDao userDao;

	@Autowired
	private UserMapper userMapper;
	/*
	 * @Author Edward
	 * @Description 查找Email是否存在
	 * @Date 23:16 2020/12/5
	 * @Param [email]
	 * @return java.lang.Integer
	 **/
	public Integer getUserByEmail(String email){
		return userDao.countUserByEmail(email);
	}
	/**
	 * @Author Edward
	 * @Description 根据用户名查找用户是否存在
	 * @Date 23:19 2020/12/5
	 * @Param [username]
	 * @return com.zlx.xBiaoShop.entity.User
	 **/
	public User isExists(String username){
		return userDao.findByUsername(username);
	}

	@Transactional
	public void updatePassword(String username,String password){
		userDao.updatePassword(username,password);
	}

	public User findUerByUsernameAndPassword(String username, String password) {
		return userDao.findUserByUsernameAndPassword(username,password);
	}
	/*
	添加图片
	 */
	@Transactional
    public void updatePic(Integer id,String pic) {
    	userDao.updatePic(id,pic);
    }
	/*
		改变默认地址
	 */
    @Transactional
	public void changeDefalutAddr(Integer userId, Integer addrId) {
		userDao.updateDefalutAddrById(userId,addrId);
	}
	/*
		修改用户信息
	 */
	@Transactional
    public void updateUserInfo(User user) {
		userMapper.updateUserInfo(user);
    }

	/**
	 * 根据wxOpenId获取用户
	 * @param wxOpenId
	 * @return
	 */
	public User selectUserByWxOpenId(String wxOpenId) {
		return userDao.getByWxOpenid(wxOpenId);
    }

	/**
	 * 保存用户
	 * @param user
	 * @return
	 */
	@Transactional
	public User save(User user){
		return userDao.save(user);
	}
	/**
	 * 根据QqOpenId获取用户
	 * @param qqOpenId
	 * @return
	 */
    public User selectUserByQqOpenId(String qqOpenId) {
    	return userDao.getByQqOpenid(qqOpenId);
    }

	public User getbyUsername(String username) {
		return userDao.findByUsername(username);
	}
}
